package service.impl;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import dao.EssayDao;
import entity.Essay;
import service.BlogService;

@Service
public class BlogServiceImpl implements BlogService {

	@Resource(name = "essayDao")
	private EssayDao essayDao;

	public EssayDao getEssayDao() {
		return essayDao;
	}

	public void setEssayDao(EssayDao essayDao) {
		this.essayDao = essayDao;
	}

	public List<Essay> getPerPage(int max, int pageNum) {
		return essayDao.getList(pageNum * max, 0, max);
	}

	public List<Essay> getAll() {
		return essayDao.getAll();
	}

	public void addNewEssay(String title, String content) {
		Essay essay = new Essay();
		essay.setTitle(title);
		essay.setComment(0);
		essay.setReading(0);
		essay.setOpen(false);
		essay.setTime(new Timestamp(new Date().getTime()));
		essay.setContent(content);

		essayDao.add(essay);
	}

	public List<Essay> getOpenEssays() {
		return essayDao.getOpenEssays();
	}

	public Essay getById(int id) {
		return essayDao.getById(id);
	}

	public void openEssay(int id) {
		essayDao.updateEssayById(id, true);
	}

	public void closeEssay(int id) {
		essayDao.updateEssayById(id, false);
	}

	public void delEssayById(int id) {
		essayDao.deleteById(id);

	}

	public void addNewEssay(Essay essay) {
		essayDao.add(essay);
	}

	public void updateById(int id, String title, String content) {
		essayDao.updateContentById(id, title,content);
	}


	@Override
	public Integer findPreviousId(int id) {
		Integer pid = essayDao.findPreId(id);
		return pid;
	}

	@Override
	public Integer findNextId(int id) {
		Integer nid= essayDao.findNextId(id);
		return nid;
	}

}
